package edu.pku.sei.mte.mtemodel.metamodel;

import java.util.List;

import edu.pku.sei.mte.mtemodel.core.NamedElement;
import edu.pku.sei.mte.mtemodel.graph.Graph;
import edu.pku.sei.mte.utilities.Visitor;

public class RTypeGraph extends Graph<RTypeClass,RTypeReference> implements NamedElement {

	private String name;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public RTypeClass getTypeClass(String name){
		List<RTypeClass> l = getNodes();
		
		for(RTypeClass t : l){
			if(t.getName().equals(name))
				return t;
		}
		
		return null;
	}
	
	public RTypeReference getTypeReference(String name, RTypeClass source,String sRole,RTypeClass target,String tRole){
		
		
		List<RTypeReference> l = this.getEdges();
		
		for(RTypeReference r : l ){
			if(name!=null&&name.length()!=0){
				if(name.equals(r.getName())==false)
					continue;
			}
			if(source!=null&&source.isA(r.getSource())==false){
				continue;
			}
			if(source!=null&&target.isA(r.getTarget())==false){
				continue;
			}
			if(sRole!=null&&sRole.length()!=0){
				if(sRole.equals(r.getSourceRole())==false)
					continue;
			}
			if(tRole!=null&&tRole.length()!=0){
				if(tRole.equals(r.getTargetRole())==false)
					continue;
			}
			
			return r;
		}
		
		return null;
	}
	
	@Override
	public Object accept(Visitor visitor) {
		return visitor.visitRTypeGraph(this);
	}
}
